<?php
	require_once("../inc/function.php");
	
function GetProducts()
{
	$conn = GetConnection();
    return $conn->query("SELECT * FROM  `Product` ");
}
function NewProduct($row)
{	
	$SKU = rand_string(20);
	$name = $row['product_name'];
	$price = $row['price'];	
	$weight = $row['weight'];
	$stock = $row['stock'];
	$productCategory = $row['product_category'];		
	$conn = GetConnection();
	$conn->query("INSERT INTO `Product` Values(NULL, '$SKU', '$name', '$price', '$weight', '$stock', '$productCategory')");
	$conn->close();	
}
function GetProduct($id)
{
	$conn = GetConnection();
    $sql = "SELECT * FROM `Product` WHERE id=$id";
    $results = $conn->query($sql);
    //echo $sql;
    $row = $results->fetch_assoc();
    $conn->close();
    return $row;	
}
function SaveProduct($row)
{
	$conn = GetConnection();
	$id = $row['id'];	
	$name = $row['product_name'];
	$price = $row['price'];	
	$weight = $row['weight'];
	$stock = $row['stock'];
	$productCategory = $row['product_category'];
	if(strlen($name)>0)
	{
		   $conn->query("UPDATE `Product` "
			.	"SET name='$name', price ='$price', weight ='$weight', stock ='$stock', Product_keywords_id='$productCategory' "
			.	"WHERE id='$id' ");        
	}
	else {
		header("Location: ../Orders/edit.php");
	}		  
       $conn->close();	
       return $error == '' ? true : array('SQL error' => $error);
}
function deleteProduct($id)
{
	$conn = GetConnection();
	$results = $conn->query("DELETE FROM `Product` WHERE id=$id");
	header('Location: http://cs.newpaltz.edu/~N01852322/Final/ProductKeywords/');
}

function rand_string( $length ) 
{
	$chars = "abcdefghijklmnopqrstuvwxyzABCDEFGHIJKLMNOPQRSTUVWXYZ0123456789";	

	$size = strlen( $chars );
	for( $i = 0; $i < $length; $i++ ) 
	{
		$str .= $chars[ rand( 0, $size - 1 ) ];
	}
	return $str;
}

function GetProductCategory()
{
	$conn = GetConnection();
    return $conn->query("SELECT * FROM  `Product_keywords` ");
}

function GetProductByCat($catID)
{
        $conn = GetConnection();
        return $conn->query("SELECT * FROM Product WHERE Product_keywords_id='$catID'");
}
?>